'''
Created on 24/03/2010

@author: eh01
'''
from datastore.resource import Resource
from datastore.spaces import Spaces
from datastore.uri import Uri
from services.makohttptemplate import MakoHTTPTemplate
from xml.dom import minidom
import unittest

site_template_1 = '''<html>
    <head></head>
    <body>${body}</body>
</html>'''

page_template_1 = '''<h1>Hello ${data_res.str}</h1>'''

page_template_2 = '''<%include file="template:external"/><p>Including template:  ${data_res.template_name}</p>'''

page_template_3 = '''<p>I will include a template that has another template.</p><%include file="template:external/a"/>'''

page_template_4 = '''<p>Test a for list</p>
<ul>
% for item in data_res.lst:
    <li>${item}</li>
% endfor
</ul>
List Index 1 is ${data_res.lst[1]}
'''

page_template_5 = '''<p>Test a for dict</p>
<ul>
% for item in data_res.dict:
    <li>${item}</li>
% endfor
</ul>
Dict value for 'Hello' is ${data_res.dict['Hello']}
'''

page_template_6 = '''<p>Test a for list and dict</p>
<ul>
% for dict in data_res.lst:
    % for (key, value) in dict.items():
        <li>${key}=${value}</li>
    % endfor
% endfor
</ul>
'''

page_template_7 = '''<p>Test if</p>
% if data_res.lst[0] > 0:
    <p>First number in lst is positive</p>
% else:
    <p>First number in lst is negative</p>
% endif
% if data_res.dict['Hello'] == 'World':
    <p>Key Hello == World</p>
% elif data_res.dict['Hello'] == 'Ed':
    <p>Key Hello == Ed</p>
% else:
    <p>Key Hello is something else</p>
% endif
'''

page_template_8 = '''<p>Test Resources</p>
% if data_res.res_1:
    <p>${data_res.res_1.get_var('datastore:hello/world')}</p>
% else:
    <p>No Resource 1</p>
% endif
% if data_res.res_2:
    <p>${data_res.res_2.lst}</p>
% else:
    <p>No Resource 2</p>
% endif
% if data_res.res_3:
    pass
% else:
    <p>No Resource 3</p>
% endif
'''

class Test(unittest.TestCase):


    def setUp(self):
        pass


    def tearDown(self):
        pass


    def test_simple_template(self):
        site_template_res = Resource(type="term:base/services/http/template")
        site_template_res.content = site_template_1
        page_template_res = Resource(type="term:base/services/http/template")
        page_template_res.content = page_template_1
        data_res = Resource(type='term:base/services/http/response')
        data_res.str = 'World'
        expected_result = '''<html>
    <head></head>
    <body><h1>Hello World</h1></body>
</html>'''
        makoHTTPTemplate = MakoHTTPTemplate(None, site_template_res, page_template_res, data_res)
        result = makoHTTPTemplate.render()
        self.assertEqual(raw_xml(expected_result), raw_xml(result))

    def test_include_template_1(self):
        site_template_res = Resource(type="term:base/services/http/template")
        site_template_res.content = site_template_1
        page_template_res = Resource(type="term:base/services/http/template")
        page_template_res.content = page_template_2
        data_res = Resource(type='term:base/services/http/response')
        data_res.str = 'World'
        data_res.template_name = 'http://www.dummy.com/template/'
        # We need a test spaces environment
        ns_dict = {'a': 'http://www.example.com/',
                   'dummy': 'http://www.dummy.com/',
                   'template': data_res.template_name}
        spaces = Spaces()
        spaces.ns = ns_dict
        external_template_res = Resource(type="term:base/services/http/template")
        external_template_res.content = page_template_1
        template_uri = Uri("template:")
        spaces.put(template_uri.external, external_template_res)
        expected_result = '''<html>
    <head></head>
    <body><h1>Hello World</h1><p>Including template:  {0}</p></body>
</html>'''.format(data_res.template_name)
        makoHTTPTemplate = MakoHTTPTemplate(spaces, site_template_res, page_template_res, data_res)
        result = makoHTTPTemplate.render()
        self.assertEqual(raw_xml(expected_result), raw_xml(result))

    def test_include_template_2(self):
        site_template_res = Resource(type="term:base/services/http/template")
        site_template_res.content = site_template_1
        page_template_res = Resource(type="term:base/services/http/template")
        page_template_res.content = page_template_3
        data_res = Resource(type='term:base/services/http/response')
        data_res.str = 'World'
        data_res.template_name = 'http://www.dummy.com/template/'
        # We need a test spaces environment
        ns_dict = {'a': 'http://www.example.com/',
                   'dummy': 'http://www.dummy.com/',
                   'template': data_res.template_name}
        spaces = Spaces()
        spaces.ns = ns_dict
        template_uri = Uri("template:")
        external_template_res_1 = Resource(type="term:base/services/http/template")
        external_template_res_1.content = page_template_1
        spaces.put(template_uri.external, external_template_res_1)
        external_template_res_2 = Resource(type="term:base/services/http/template")
        external_template_res_2.content = page_template_2
        spaces.put(template_uri.external.a, external_template_res_2)
        expected_result = '''<html>
    <head></head>
    <body><p>I will include a template that has another template.</p><h1>Hello World</h1><p>Including template:  {0}</p></body>
</html>'''.format(data_res.template_name)
        makoHTTPTemplate = MakoHTTPTemplate(spaces, site_template_res, page_template_res, data_res)
        result = makoHTTPTemplate.render()
        self.assertEqual(raw_xml(expected_result), raw_xml(result))

    def test_list_template(self):
        site_template_res = Resource(type="term:base/services/http/template")
        site_template_res.content = site_template_1
        page_template_res = Resource(type="term:base/services/http/template")
        page_template_res.content = page_template_4
        data_res = Resource(type='term:base/services/http/response')
        data_res.lst = [1, 2, 3]
        expected_result = '''<html>
    <head></head>
    <body><p>Test a for list</p>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    List Index 1 is 2</body>
</html>'''
        makoHTTPTemplate = MakoHTTPTemplate(None, site_template_res, page_template_res, data_res)
        result = makoHTTPTemplate.render()
        self.assertEqual(raw_xml(expected_result), raw_xml(result))

    def test_dict_template(self):
        site_template_res = Resource(type="term:base/services/http/template")
        site_template_res.content = site_template_1
        page_template_res = Resource(type="term:base/services/http/template")
        page_template_res.content = page_template_5
        data_res = Resource(type='term:base/services/http/response')
        data_res.dict = {'Hello':'World',
                         123:456}
        expected_result = '''<html>
    <head></head>
    <body><p>Test a for dict</p>
    <ul>
        <li>123</li>
        <li>Hello</li>
    </ul>
    Dict value for 'Hello' is World</body>
</html>'''
        makoHTTPTemplate = MakoHTTPTemplate(None, site_template_res, page_template_res, data_res)
        result = makoHTTPTemplate.render()
        self.assertEqual(raw_xml(expected_result), raw_xml(result))

    def test_list_dict_template(self):
        site_template_res = Resource(type="term:base/services/http/template")
        site_template_res.content = site_template_1
        page_template_res = Resource(type="term:base/services/http/template")
        page_template_res.content = page_template_6
        data_res = Resource(type='term:base/services/http/response')
        data_res.lst = [{'HAF':'eDA',0:1},{'Hello':'World',123:456}]
        expected_result = '''<html>
    <head></head>
    <body><p>Test a for list and dict</p>
    <ul>
        <li>0=1</li>
        <li>HAF=eDA</li>
        <li>123=456</li>
        <li>Hello=World</li>
    </ul>
    </body>
</html>'''
        makoHTTPTemplate = MakoHTTPTemplate(None, site_template_res, page_template_res, data_res)
        result = makoHTTPTemplate.render()
        self.assertEqual(raw_xml(expected_result), raw_xml(result))

    def test_if_template(self):
        site_template_res = Resource(type="term:base/services/http/template")
        site_template_res.content = site_template_1
        page_template_res = Resource(type="term:base/services/http/template")
        page_template_res.content = page_template_7
        data_res = Resource(type='term:base/services/http/response')
        data_res.lst = [1, -1, 2]
        data_res.dict = {'Hello':'Anyone'}
        expected_result = '''<html>
    <head></head>
    <body><p>Test if</p>
    <p>First number in lst is positive</p>
    <p>Key Hello is something else</p>
    </body>
</html>'''
        makoHTTPTemplate = MakoHTTPTemplate(None, site_template_res, page_template_res, data_res)
        result = makoHTTPTemplate.render()
        self.assertEqual(raw_xml(expected_result), raw_xml(result))

    def test_resource_template(self):
        site_template_res = Resource(type="term:base/services/http/template")
        site_template_res.content = site_template_1
        page_template_res = Resource(type="term:base/services/http/template")
        page_template_res.content = page_template_8
        # We need a test spaces environment
        data_res = Resource(type='term:base/services/http/response')
        res_1 = Resource(type="dummy")
        res_1.set_var('datastore:hello/world', 'HAFeDA')
        res_2 = Resource(type="dummy")
        res_2.lst = [1,2,3]
        data_res.res_1 = res_1
        data_res.res_2 = res_2
        data_res.res_3 = None
        expected_result = '''<html>
    <head></head>
    <body>
    <p>Test Resources</p>
    <p>HAFeDA</p>
    <p>[1, 2, 3]</p>
    <p>No Resource 3</p>
    </body>
</html>'''
        makoHTTPTemplate = MakoHTTPTemplate(None, site_template_res, page_template_res, data_res)
        result = makoHTTPTemplate.render()
        self.assertEqual(raw_xml(expected_result), raw_xml(result))

def raw_xml(xml_str):
    xml = minidom.parseString(xml_str)
    return u''.join([unicode(line).strip() for line in xml.toprettyxml().splitlines()])

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
